import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';

@Component({
  selector: 'app-permissions-cnc',
  templateUrl: './permissions-cnc.component.html',
  styleUrls: ['./permissions-cnc.component.scss']
})
export class PermissionsCNCComponent implements OnInit {
  id;
  isVisible;
  listOfData = []; // 设备信息的数组
  name; // 几号机台
  MachineModel; // 设备型号
  MachineName; // 设备名称
  MachineSN; // 序列号
  widthConfig = ['150px', '150px', '150px', '300px', '150px'];
  scrollConfig = { x: '900px', y: '440px' };

  constructor(public axios: AxiosService) { }

  ngOnInit(): void {
    this.queryCNC();
  }

  // 查看cnc设备
  queryCNC() {
    this.axios.axiosPost('QueryCncInfo')
      .then((data) => {
        if (data) {
          console.log(data);
          this.listOfData = data['order'];
          this.listOfData.sort((a, b) => {
            return a.name > b.name ? 1 : -1;
          });
          console.log(this.listOfData);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }

  // 修改
  edit(data) {
    this.MachineModel = data.MachineModel;
    this.MachineName = data.MachineName;
    this.name = data.name;
    this.MachineSN = data.MachineSN;
    this.id = data.id;
    console.log(data);
  }

  // 清除input中的信息
  clear() {
    this.name = '';
    this.MachineModel = '';
    this.MachineName = '';
    this.MachineSN = '';
  }

  // 删除设备
  handleOk() {
    this.axios.axiosPost('DeleteCncInfo', { id: this.id })
      .then((data) => {
        if (data['result'] === 'ok') {
          this.queryCNC();
          console.log(data);
        }
      })
      .catch((err) => {
        console.log(err);
      });
    this.isVisible = false;
  }
  showModal(data): void {
    this.id = data.id;
    console.log(this.id);
    this.isVisible = true;
  }
  handleCancel(): void {
    this.isVisible = false;
  }

  // 保存设备信息
  save() {
    debugger
    let arr = [];
    for (let i = 0; i < this.listOfData.length; i++) {
      arr.push(this.listOfData[i]['MachineName'])
    }
    if (arr.includes(this.MachineName)) {
      console.log('更新')
      console.log('____________________---------------', this.id)
      this.axios.axiosPost('UpdateCncInfo', { id: this.id, name: this.name, MachineModel: this.MachineModel, MachineName: this.MachineName, MachineSN: this.MachineSN })
        .then((data) => {
          console.log(data);
          if (data['result'] === 'ok') {
            this.queryCNC();
            console.log(data);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    } else {
      console.log('增加')
      this.axios.axiosPost('AddCncInfo', { name: this.name, MachineModel: this.MachineModel, MachineName: this.MachineName, MachineSN: this.MachineSN })
        .then((data) => {
          console.log(data);
          if (data['result'] === 'ok') {
            this.queryCNC();
            console.log(data);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    }
  }

}
